package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 营业额统计
     *
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
        ArrayList<LocalDate> list = new ArrayList<>();
        list.add(begin);
        while (begin.isBefore(end)) {
            begin = begin.plusDays(1);
            list.add(begin);
        }
        List<Double> sum = new ArrayList<>();
        for (LocalDate date : list) {
            LocalDateTime minTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime maxTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("begin", minTime);
            map.put("end", maxTime);
            map.put("status", Orders.COMPLETED);

            Double s = ordersMapper.sumByMap(map);
            s = s == null ? 0.0 : s;
            sum.add(s);
        }


        String join = StringUtils.join(list, ",");
        String strSum = StringUtils.join(sum, ",");
        TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder().dateList(join).turnoverList(strSum).build();

        return turnoverReportVO;
    }

    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();

        dateList.add(begin);
        while (begin.isBefore(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        //创建一个集合用来存放每天用户总数
        List<Integer> totalUser = new ArrayList<>();
        //创建一个集合用来存放新增用户总数
        List<Integer> newUser = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Map map = new HashMap();
            map.put("end", endTime);
            Integer totalU = userMapper.countByMap(map);
            map.put("begin", beginTime);
            Integer newU = userMapper.countByMap(map);
            totalUser.add(totalU);
            newUser.add(newU);
        }
        UserReportVO reportVO = UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .totalUserList(StringUtils.join(totalUser, ","))
                .newUserList(StringUtils.join(newUser, ","))
                .build();
        return reportVO;
    }

    @Override
    public OrderReportVO orderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (begin.isBefore(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        List<Integer> orderCountList = new ArrayList<>();
        List<Integer> validOrderCountList = new ArrayList<>();
        for (LocalDate date : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);

            Map map = new HashMap();
            map.put("begin", beginTime);
            map.put("end", endTime);
            Integer orderCount = ordersMapper.orderSum(map);
            map.put("status", Orders.COMPLETED);
            Integer validOrderCount = ordersMapper.orderSum(map);
            orderCountList.add(orderCount);
            validOrderCountList.add(validOrderCount);
        }
        String s = dateList.stream().map(new Function<LocalDate, String>() {
            @Override
            public String apply(LocalDate localDate) {
                return localDate.toString() + ",";
            }
        }).collect(Collectors.joining());

        Integer orderCount = orderCountList.stream().reduce((a, b) -> a + b).get();
        Integer validOrderCount = validOrderCountList.stream().reduce(Integer::sum).get();
        Double orderCompletionRate = 0.0;
        if (orderCount != 0) {
            orderCompletionRate = validOrderCount.doubleValue() / orderCount;
        }
        OrderReportVO reportVO = OrderReportVO.builder().orderCountList(StringUtils.join(orderCountList, ","))
                .validOrderCountList(StringUtils.join(validOrderCountList, ","))
                .dateList(s)
                .totalOrderCount(orderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
        return reportVO;
    }

    @Override
    public SalesTop10ReportVO salesTop(LocalDate begin, LocalDate end) {
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);

        List<GoodsSalesDTO> list = orderDetailMapper.salesTop(beginTime,endTime);
        List<String> name = list.stream().map(s -> s.getName()).collect(Collectors.toList());
        List<Integer> number = list.stream().map(s -> s.getNumber()).collect(Collectors.toList());
        String nameList = StringUtils.join(name, ",");
        String numberList = StringUtils.join(number, ",");
        SalesTop10ReportVO salesTop10ReportVO = SalesTop10ReportVO.builder().nameList(nameList).numberList(numberList).build();
        return salesTop10ReportVO;
    }
}
